package auth

import (
	"github.com/martini-contrib/render"
	// "log"
	"net/http"
	"server/lib/md5"
	"server/lib/session"
	"server/web/model/shop/admin/auth"
	// "fmt"
	// "server/conf"
	"server/lib/Imartini"
)

func Login(r render.Render, res http.ResponseWriter, req *http.Request, mlog *Imartini.Mlog) {
	ret := map[string]interface{}{}

	ret["msg"] = req.FormValue("msg")
	r.HTML("shop/admin/login.html", ret)
}

func PLogin(r render.Render, res http.ResponseWriter, req *http.Request, mlog *Imartini.Mlog, sess session.SESS) {
	url := req.FormValue("url")
	username := req.FormValue("username")
	password := req.FormValue("password")

	if username == "" || password == "" {
		http.Redirect(res, req, "/admin/login?msg=用户名密码不能为空", http.StatusFound)
		return
	}

	i, dauth := auth.Login(username, md5.Md5(password))
	if i == 1 {
		s := sess
		s["admin_user"] = username
		s["admin_passwd"] = md5.Md5(password)
		s["admin_info"] = dauth
		if url != "" {
			http.Redirect(res, req, url, http.StatusFound)
		} else {
			http.Redirect(res, req, "/admin/", http.StatusFound)
		}
	} else {
		http.Redirect(res, req, "/admin/login?msg=用户名或密码错误", http.StatusFound)
	}
}
